package com.fight.strive.sys.modules.scheduler.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fight.strive.sys.modules.common.PageR;
import com.fight.strive.sys.modules.mybatis.MyBatisPlusUtils;
import com.fight.strive.sys.modules.scheduler.dao.SchRecordMapper;
import com.fight.strive.sys.modules.scheduler.constants.SchConstants;
import com.fight.strive.sys.modules.scheduler.entity.SchJobEntity;
import com.fight.strive.sys.modules.scheduler.entity.SchRecordEntity;
import com.fight.strive.sys.modules.scheduler.service.SchRecordService;
import com.fight.strive.sys.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Slf4j
@Service
@DS("master")
public class SchRecordServiceImpl
        extends ServiceImpl<SchRecordMapper, SchRecordEntity>
        implements SchRecordService {

    @Override
    public SchRecordEntity saveJobRecord(SchJobEntity job) {
        SchRecordEntity entity = new SchRecordEntity();
        entity.setJobId(job.getId())
                .setJobCode(job.getCode())
                .setJobName(job.getName())
                .setStatus(SchConstants.RUNNING)
                .setStartTime(LocalDateTime.now());
        this.save(entity);
        return entity;
    }

    @Override
    public void updateJobRecord(SchRecordEntity entity) {
        entity.setEndTime(LocalDateTime.now());
        this.saveOrUpdate(entity);
    }

    @Override
    public IPage<SchRecordEntity> list(
            PageR<SchRecordEntity> page) {
        QueryWrapper<SchRecordEntity> queryWrapper = new QueryWrapper<>();
        MyBatisPlusUtils.buildAndQuery(queryWrapper, page.getCond(),
                "startTime", "endTime");
        queryWrapper.ge(ObjectUtils.notNull(page.getCond().get("startTime")),
                "create_time", page.getCond().get("startTime"))
                .le(ObjectUtils.notNull(page.getCond().get("endTime")),
                        "create_time", page.getCond().get("endTime"));
        return this.page(page, queryWrapper);
    }

    @Override
    public void clearRecord(String ago) {
        QueryWrapper<SchRecordEntity> queryWrapper = new QueryWrapper<>();
        LocalDate localDate = null;
        switch (ago) {
            case "day":
                // 清除1天之前
                localDate = LocalDate.now().minusDays(1);
                break;
            case "week":
                // 清除1周之前
                localDate = LocalDate.now().minusWeeks(1);
                break;
            case "month":
                // 清除1月之前
                localDate = LocalDate.now().minusMonths(1);
                break;
            case "year":
                // 清除1年之前
                localDate = LocalDate.now().minusYears(1);
                break;
            default:
                // 全部清除
                break;
        }
        if (ObjectUtils.notNull(localDate)) {
            queryWrapper.le(
                    "date(create_time)", localDate);
        }
        this.remove(queryWrapper);
    }
}
